package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import com.itheima.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 编写部门查询功能 无条件查询
     * @return
     */
    @GetMapping()
    public Result selectDept(){
        List<Dept> listdept=deptService.selectDept();
        return Result.success(listdept);
    }

    /**
     * 编写根据id删除部门
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) throws IOException {
        deptService.deleteById(id);
        return Result.success();
    }

    /**
     * 添加员工 根据名字 注意要设置 不能为空的值
     * @param dept
     * @return
     */
    @PostMapping()
    public Result insertByName(@RequestBody Dept dept){
        deptService.insertByName(dept);
        return Result.success();
    }

    /**
     * 根据id 查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result seleceById(@PathVariable Integer id){
        Dept dept=deptService.selectById(id);
        return Result.success(dept);
    }

    /**修改
     * @param dept
     * @return
     */
    @PutMapping
    public Result putByList(@RequestBody Dept dept){
        deptService.putByList(dept);
        return Result.success();
    }


}
